import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//对应一个客户端的聊天通道类
public class ChatChannel implements Runnable {
    static List<Socket> socketList=new ArrayList<Socket>();
    //定义static的Map<String,ObjectOutputStream>对象allMap
    static Map<String,ObjectOutputStream> allMap = new HashMap<String, ObjectOutputStream>();
    //定义集合类对象all存储服务器端所有对应客户端的socket的输出流对象
    static List<ObjectOutputStream> all = new ArrayList<ObjectOutputStream>();
    private Socket socket;
    //接收需要输入流
    private ObjectInputStream ois;
    //能够转发消息需要输出流
    private ObjectOutputStream oos;
    private String name;

    public ChatChannel(ObjectInputStream ois, ObjectOutputStream oos, String name) {
        this.ois = ois;
        this.oos = oos;
        this.name = name;
        all.add(oos);
        //创建通道对象时，把该客户端的名称-输出流键值对添加道allMap中
        allMap.put(name,oos);
    }
    public ChatChannel(List<Socket> socketList, String name) {
        this.socketList = socketList;
        this.name = name;
        for (int i = 0; i < socketList.size(); i++){
            socket=socketList.get(i);
            try {
                oos = new ObjectOutputStream(socket.getOutputStream());
                ois = new ObjectInputStream(socket.getInputStream());
                all.add(oos);
                //创建通道对象时，把该客户端的名称-输出流键值对添加道allMap中
                allMap.put(name,oos);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void run() {
        while (true){
            try {
                //读取客户端的信息
                String msg = (String) ois.readObject();
                //将读取到的信息在服务器端打印出来
                System.out.println(name+"说:"+msg);
                //判断是否私聊
                //msg是否以@开头startWith("@")
                //含有#字符indexOf（"#")>0
                int endIndex = msg.indexOf("#");
                if (msg.startsWith("@")&&msg.indexOf("#")>0){
                    //获得私聊对象的名称substring(1,indexOf("#"))
                    String secretName = msg.substring(1,endIndex);
                    //通过私聊对象的名称secretName获得对应聊天通道的输出流对象
                    ObjectOutputStream secretOOs = allMap.get(secretName);
                    ObjectOutputStream oos = allMap.get(this.name);
                    if (secretOOs != null){
                        secretOOs.writeObject(name+"悄悄对你说："+msg.substring(endIndex+1));
                        oos.writeObject("你"+"悄悄对"+secretName+"说："+msg.substring(endIndex+1));
                    }
                }
                else {
                    //ObjectOutputStream outputStream = allMap.get(name);
                    for (ObjectOutputStream oos : all){//int i = 0; i < socketList.size(); i++
                         oos.writeObject(name+"说:"+msg);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    socket.close();
                    allMap.remove(name);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
